package com.example.pei.ithinklearnt.model;

import com.example.pei.ithinklearnt.bean.AlumniCircle;
import com.example.pei.ithinklearnt.bean.Comment;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Pei on 2017/12/15.
 */

public class CommentModelImp implements  CommentModel{

    @Override
    public void queryComments(AlumniCircle al, final queryComentsListener listener) {
        BmobQuery<Comment> query = new BmobQuery<Comment>();
        query.addWhereEqualTo("dynamic", al);
        query.include("userFrom,userTo,dynamic");   //需要查找到评论者的信息 回复的人的信息
        query.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> object, BmobException e) {
                if (e == null) {
                    listener.queryComentsSucceed(object);   //回调监听
                } else {
                    listener.queryComentsFailed();     //失败
                }
            }
        });
    }

    @Override
    public void deleteComment(String objectId, final deleteCommentListener listener) {
        Comment comment=new Comment();
        comment.setObjectId(objectId);
        comment.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    listener.succeed();
                }else{
                    listener.failed();
                }
            }
        });
    }

    public interface queryComentsListener{
        void queryComentsSucceed(List<Comment> results);   //成功获取评论
        void queryComentsFailed();  // 失败
    }
    public interface deleteCommentListener{
        void succeed();
        void failed();
    }
}
